<script setup lang="ts">
import {computed, markRaw, onMounted, ref, watch} from "vue";
import type {EChartsOption, EChartsType} from "echarts";
import * as echarts from "echarts";
import {useGetMapDataApi} from "@/api";
import type {CommonTow, Map} from "@/interface";
import 'echarts/extension/bmap/bmap'
import {log} from "echarts/types/src/util/log";


interface Emits {
  (e: 'click', name: string): void;
}

interface Props {
  name: string;
}

const map = ref()
const option = ref<EChartsOption>()
const myChart = ref<EChartsType>()
const mapData = ref<Map>({
  data: []
})
const props = defineProps<Props>()
const emits = defineEmits<Emits>()
const iconImg = "image://\n"
const init = async () => {
  const {data: res} = await useGetMapDataApi()
  mapData.value = res
  myChart.value = markRaw(echarts.init(map.value));
  draw();
}
const draw = () => {
  const data = mapData.value.data.filter((value:CommonTow) => value.name === props.name)
  console.log(data[0].value)
  // const location = [parseFloat(data[0].value[0]) + 0.0022, parseFloat(data[0].value[1]) - 0.0008]
  const location = [data[0].value[0], data[0].value[1]]
  option.value = {
    tooltip: {},
    bmap: {
      key: 'kUhVhAvFdXUXFZhM0rQYhPHGduddLPGF',
      center: location,
      zoom: 20,
      roam: true
    },
    series: [
      {
        name: '景点/评分', // tip标题
        type: 'effectScatter',
        coordinateSystem: 'bmap', // 坐标系统 百度地图
        data: mapData.value.data,
        encode: {
          value: 2 // 定制tip value
        },
        itemStyle: {
          color: '#0cb4d1', // 点的颜色
          shadowBlur: 10,
          shadowColor: '#333'
        },
        symbol: iconImg,
        symbolSize: function () {
          return 16
        },
        label: {
          show: false, // 显示value
          position: 'right',
          // formatter: '{b}', // 将显示的value改为标题
          formatter: function (val) {
            return `${val.data.name} - ${val.data.value[2]}`
          }
        },
        emphasis: {
          label: {
            show: true // 将label中的show置为false, 通过在这里的设置，让鼠标移上去再显示label内容
          }
        },
        hoverAnimation: true, // 鼠标移动到点上
        rippleEffect: {
          brushType: 'stroke' // 波纹样式 空心
        }
      }
    ]
  }

  myChart.value.setOption(option.value, {notMerge: true});
  myChart.value.on('click', (params) => {
    // console.log('name:', params.name);
    // console.log('seriesName:', params.seriesName);
    // console.log('value:', params.value);
    // console.log('dataIndex:', params.dataIndex);
    // console.log('data:', params.data);
    emits('click', params.name)
  })
}
watch(
    () => props.name,
    (value, oldValue) => {
      draw()
    }
)
onMounted(() => {
  init()
})
</script>

<template>
  <div ref="map" class="map">

  </div>
</template>

<style scoped>
.map {
  width: 100%;
  height: 100%;
}
</style>